package com.hsck.order.kds.setup.fragment.cxzd

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.hjq.toast.Toaster
import com.hsck.order.kds.R
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.bean.SetupBean
import com.hsck.order.kds.base.isnoam.network.livedata.LiveDataBus
import com.hsck.order.kds.base.utils.LogUtil
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.databinding.FragmentYytsBinding
import com.hsck.order.kds.setup.SetupActivity
import com.hsck.order.kds.setup.adapter.KeyboardAdapter
import com.hsck.order.kds.setup.adapter.YytsAdapter
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.common_head_black.*
import kotlinx.android.synthetic.main.dialog_guqing.view.*
import kotlinx.android.synthetic.main.yujingtixing.*

/**
 * 语音提示
 */
class YytsFragment : BaseFragment<FragmentYytsBinding>(), SetupActivity.UpdatableFragment {


    var type = ""
    var topFragment: Fragment? = null
    private val adapter by lazy { YytsAdapter(R.layout.item_yyts) }
    private val adapterNum by lazy { KeyboardAdapter(R.layout.item_keyboard) }

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentYytsBinding {
        return FragmentYytsBinding.inflate(inflater)
    }

    fun setBack() {
        if (topFragment != null) {
            val activity = requireActivity() as SetupActivity
            activity.switchFragment(topFragment!!)
        }
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        lin_head_black_back.setOnClickListener {
            setBack()
        }
        tv_head_black_title.text = "语音提示"
        binding.apply {
            rvYyts.layoutManager = GridLayoutManager(requireActivity(), 2)
            rvYyts.adapter = adapter
            adapter.setMultipleChoice(R.id.cb_item_yyts, false)
            val list = arrayListOf<SetupBean>()
            for (i in 1..6) {
                list.add(SetupBean("退菜提醒$i", i, false))
            }
            adapter.datas = list

            adapter.onPositionClick = { it, index ->
                adapter.notifyDataSetChanged()
            }
            adapter.onNumClick = { it, index ->
                dlYyts.openDrawer(GravityCompat.END)
                setDl(it)
            }
            //确认
            btnYytsQr.setOnClickListener {
                val list = adapter.getSelect() as ArrayList<SetupBean>
                    //给上级页面传值
                    LiveDataBus.with<ArrayList<SetupBean>>("YytsFragment").postData(list)
                    setBack()
            }
        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }

    private fun setDl(data: SetupBean) {
        binding.dlYyts.addDrawerListener(object : DrawerLayout.DrawerListener {
            override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
            }

            override fun onDrawerOpened(drawerView: View) {
                LogUtil.i("onDrawerOpened")
                drawerView.isClickable = true
            }

            override fun onDrawerClosed(drawerView: View) {
            }

            override fun onDrawerStateChanged(newState: Int) {
            }
        })

        rv_yujingtixing.layoutManager = GridLayoutManager(activity, 3)
        rv_yujingtixing.adapter = adapterNum
        tv_yujingtixing_num.text =data.code.toString()

        val list = arrayListOf<String>()
        for (i in 1..12) {
            if (i < 10) {
                list.add(i.toString())
            } else if (i == 10) {
                list.add("清空")
            } else if (i == 11) {
                list.add("0")
            } else if (i == 12) {
                list.add("删除")
            }
        }
        adapterNum.datas = list

        adapterNum.onItemClick = {
            when (it) {
                "清空" -> {
                    tv_yujingtixing_num.text = ""
                }
                "删除" -> {
                    val num = tv_yujingtixing_num.text.toString().trim()
                    if (num != "") {
                        tv_yujingtixing_num.text =
                            num.substring(0, num.length - 1)
                    }
                }
                "0" -> {
                    if (tv_yujingtixing_num.text.toString().trim() != "") {
                        val num = tv_yujingtixing_num.text.toString().trim()
                        tv_yujingtixing_num.text = "$num$it"
                    }
                }
                else -> {
                    val num = tv_yujingtixing_num.text.toString().trim()
                    tv_yujingtixing_num.text = "$num$it"
                }
            }
        }
        //关闭
        img_yujingtixing_close.setOnClickListener {
            binding.dlYyts.closeDrawer(GravityCompat.END)
        }
        //确认修改
        btn_yujingtixing.setOnClickListener {
            val num = tv_yujingtixing_num.text.toString().trim()
            if (num != "0" && num != "") {
                try {
                    data.code = num.toInt()
                    adapter.notifyDataSetChanged()
                    binding.dlYyts.closeDrawer(GravityCompat.END)
                }catch (e:Exception){
                    Toaster.show("请检查数据合法性")
                }
            }
        }

    }


}